Skip to content

sys/battery: add hysteresis to full level check#483

Merged
laurensvalk merged 1 commit into
workfrom
copilot/feature-battery-hysteresis
Jul 14, 2026
Merged

sys/battery: add hysteresis to full level check#483
laurensvalk merged 1 commit into
workfrom
copilot/feature-battery-hysteresis

Conversation

Copilot AI commented Jul 14, 2026

Copy link
Copy Markdown

Battery voltage fluctuates near the full threshold, causing the charge status light to flicker between red (charging) and green (full).

Changes

  • LIION_FULL_HYSTERESIS_MV macro — 100 mV (50 mV/cell) hysteresis band around the full threshold
  • pbsys_battery_is_full() hysteresis logic — uses a static bool was_full to implement a Schmitt-trigger-style check: transitions to full at LIION_FULL_MV (8190 mV), stays full until voltage drops below LIION_FULL_MV - LIION_FULL_HYSTERESIS_MV (8090 mV)
bool pbsys_battery_is_full(void) {
    static bool was_full;
    int32_t voltage = pbio_battery_get_average_voltage();
    if (was_full) {
        was_full = voltage >= LIION_FULL_MV - LIION_FULL_HYSTERESIS_MV;
    } else {
        was_full = voltage >= LIION_FULL_MV;
    }
    return was_full;
}

The static variable is zero-initialized by the C standard, so on first call the strict >= LIION_FULL_MV threshold applies — correctly detecting a battery that is already full at boot.

Copilot AI changed the title [WIP] Add hysteresis for battery charge level check sys/battery: add hysteresis to full level check Jul 14, 2026
Copilot finished work on behalf of dlech July 14, 2026 00:31
Copilot AI requested a review from dlech July 14, 2026 00:31
Comment thread lib/pbio/sys/battery.c
Copilot finished work on behalf of dlech July 14, 2026 00:35
Copilot AI requested a review from dlech July 14, 2026 00:35
@dlech
dlech marked this pull request as ready for review July 14, 2026 01:05
@dlech
dlech requested a review from laurensvalk July 14, 2026 01:05
Avoids the light blinking red then green a few times before staying solid on
green
@laurensvalk
laurensvalk force-pushed the copilot/feature-battery-hysteresis branch from 35be43f to 293c211 Compare July 14, 2026 06:58
@laurensvalk
laurensvalk changed the base branch from master to work July 14, 2026 06:59
@laurensvalk
laurensvalk merged commit 293c211 into work Jul 14, 2026
28 checks passed
@dlech
dlech deleted the copilot/feature-battery-hysteresis branch July 14, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Battery charge light needs hysteresis on full level check

3 participants